# -*- coding: utf-8 -*-
# article_info.py
# Created by Hardy on 26th, Jan
# Copyright 2017 杭州网川教育有限公司. All rights reserved.

import happybase


class ImageHBASESaver:
    def __init__(self, host, table):
        self.host = host
        self.table = table

    def search(self, args):
        img_id = args.get('img_id')
        if img_id is None:
            raise ValueError('message: "img_id" is needed')

        img_id = img_id.replace('.jpg', '')

        hbase_connection = happybase.Connection(self.host)
        hbase_table = hbase_connection.table(bytes(self.table, encoding='utf-8'))

        hbase_table.put(bytes(img_id, encoding='utf-8'))

        image = hbase_table.row(bytes(img_id, encoding='utf-8'))
        result = image.get(b'pic_entry:context', b'')
        # print(result.decode())
        hbase_connection.close()
        return result

    def _build_query(self, args): pass

    def _build_result(self, result, param): pass
